import {
  Vector2,
  Color,
  WebGLRenderTarget,
  LinearFilter,
  RGBAFormat,
  sRGBEncoding,
} from 'three'
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer'
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass'
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass'
// import { RGBShiftShader } from 'three/examples/jsm/shaders/RGBShiftShader'
import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass'
import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader'
// import { DotScreenPass } from 'three/examples/jsm/postprocessing/DotScreenPass'
// import { GlitchPass } from 'three/examples/jsm/postprocessing/GlitchPass'
// import { SMAAPass } from 'three/examples/jsm/postprocessing/SMAAPass'
import { UnrealBloomPass } from 'three/examples/jsm/postprocessing/UnrealBloomPass'

export class createComposer {
  composer
  constructor(renderer, width, height, scene, camera) {
    //创建渲染缓冲区
    const renderTarget = new WebGLRenderTarget({
      minFilter: LinearFilter,
      magFilter: LinearFilter,
      format: RGBAFormat,
      encoding: sRGBEncoding, //修复颜色
      // generateMipmaps: true,
    })
    renderTarget.texture.name = 'EffectComposer.rt'
    // 创建效果合成器
    const composer = new EffectComposer(renderer, renderTarget)
    composer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) //渲染像素比
    composer.setSize(width, height) //渲染精度

    // 渲染通道，该通道会渲染场景，但不会渲染到屏幕上
    const renderPass = new RenderPass(scene, camera)
    composer.addPass(renderPass)

    // // rgb散色通道
    // const rgbShiftPass = new ShaderPass(RGBShiftShader)
    // composer.addPass(rgbShiftPass)

    // // 黑白光栅化通道
    // const dotScreenPass = new DotScreenPass()
    // composer.addPass(dotScreenPass)

    // // 故障效果通道
    // const glitchPass = new GlitchPass()
    // composer.addPass(glitchPass)

    // 发光通道
    const unrealBloomPass = new UnrealBloomPass()
    composer.addPass(unrealBloomPass)
    unrealBloomPass.strength = 0.3 //光照强度
    unrealBloomPass.radius = 1 //亮度发散半径
    unrealBloomPass.threshold = 0.3 //限制物体开始发光的亮度

    // //   抗锯齿通道(消耗性能)
    // const smmaPass = new SMAAPass()
    // composer.addPass(smmaPass)

    const outlinePass = new OutlinePass(
      new Vector2(width, height),
      scene,
      camera
    )
    composer.addPass(outlinePass)

    // ShaderPass通道是将渲染结果渲染到屏幕上
    const effectFXAA = new ShaderPass(FXAAShader)
    effectFXAA.uniforms.resolution.value.set(1 / width, 1 / height)
    composer.addPass(effectFXAA)

    outlinePass.visibleEdgeColor.set(new Color('red')) // 边缘可见部分发颜色 sColor[0].color
    outlinePass.hiddenEdgeColor.set(new Color('white')) // 边缘遮挡部分发光颜色 sColor[1].color
    outlinePass.edgeStrength = Number(10.0) //边框的亮度
    // outlinePass.edgeGlow = Number(1) //光晕[0,1]
    // outlinePass.edgeThickness = Number(1.0) //边缘浓度
    // outlinePass.pulsePeriod = Number(1.8) //呼吸闪烁的速度 闪烁频率 ，默认0 ，值越大频率越低
    // outlinePass.usePatternTexture = false //是否使用父级的材质
    // outlinePass.downSampleRatio = 2 // 边框弯曲度
    outlinePass.clear = true

    // 自定义通道
    // 着色通道
    const TintShader = {
      uniforms: {
        //获取上一个通道的纹理
        tDiffuse: {
          value: null,
        },
      },
      vertexShader: `
        varying vec2 vUv;
        void main(){
          gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
          vUv = uv;
        }
      `,
      // 检索像素 tDiffuse 纹理贴; vUv uv坐标
      fragmentShader: `
          uniform sampler2D tDiffuse;
          varying vec2 vUv;
          void main(){
            vec4 color = texture2D(tDiffuse,vUv);
            color.r += 0.01;
            gl_FragColor  = color;
        }
      `,
    }
    const tinitPass = new ShaderPass(TintShader)
    composer.addPass(tinitPass)

    this.composer = composer
    this.outlinePass = outlinePass
  }
  addSelectObject(objectArr) {
    this.outlinePass.selectedObjects = objectArr.length ? objectArr : []
  }
}
